<template>
  <div>
    <el-drawer
      title="修改面经"
      :visible.sync="dialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
      size="50%"
    >
      <div class="demo-drawer__content">
        <el-form :model="form" :rules="rules" ref="ruleForm">
          <el-form-item label="标题" label-width="80px" prop="stem">
            <el-input v-model="form.stem" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="内容" label-width="80px" prop="content">
            <quill-editor
              v-model="form.content"
              ref="myQuillEditor"
              :options="editorOption"
              class="editor"
            ></quill-editor>
          </el-form-item>
          <el-form-item class="button">
            <el-button type="primary" @click="submitForm('ruleForm')">确认</el-button>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
// eslint-disable-next-line
import { updataDetailApi, detailArticalApi ,createArticalApi} from '@/api/list'

// 工具栏配置项
const toolbarOptions = [
  // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  ['bold', 'italic', 'underline', 'strike'],
  // 引用  代码块-----['blockquote', 'code-block']
  ['blockquote', 'code-block'],
  // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  [{ list: 'ordered' }, { list: 'bullet' }],
  // 清除文本格式-----['clean']
  ['clean'],
  // 链接、图片、视频-----['link', 'image', 'video']
  ['image', 'video', 'link']
]

export default {
  name: 'ModifyAritical',
  props: {},
  components: {
    quillEditor
  },
  data () {
    return {
      dialog: false,
      id: '',
      artical: {},
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      // 富文本
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions
          }
        }
      }
    }
  },
  computed: {},
  watch: {},
  created () {
  },
  mounted () {},
  methods: {
    // handleClose (done) {
    //   done()
    // },
    async open (id) {
      this.dialog = true
      if (!id) {
        this.form = {
          stem: '',
          content: ''
        }
        return
      }
      this.id = id
      console.log(this.id)
      const { data } = await detailArticalApi(id)
      this.form = data
    },
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) {
          return
        }
        // axios
        if (this.form.id) {
          await updataDetailApi({
            id: this.form.id,
            stem: this.form.stem,
            content: this.form.content
          })
          this.$message({
            message: '编辑成功',
            type: 'success'
          })
        } else {
          await createArticalApi({
            stem: this.form.stem,
            content: this.form.content
          })
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        }
        this.$emit('updata')
        this.dialog = false
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
      this.dialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.demo-drawer__content {
  padding: 0 20px;
}
::v-deep .ql-container {
  height: 200px;
}
::v-deep .button .el-form-item__content {
  text-align: center;
}
</style>
